import 'package:flutter/foundation.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/device/device_const.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';

import 'sleep_setting_state.dart';

class SleepSettingLogic extends BaseController {

  static const setSciSleep = "setSciSleep";
  static const isSciSleep = "isSciSleep";

  final SleepSettingState state = SleepSettingState();

  void setSleepEnable(value) {
    showLoading();
    state.isSciSleep.value = value;
    bRemoteReqAsync(setSciSleep, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetSciSleep(success, value));
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.isSciSleep.value = read(isSciSleep, defaultValue: false);
    addStorageListener(DeviceConst.sleepStartTime, (value) {
      state.startTime.value = value;
    });
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  void _onSetSciSleep(success, value) {
    cancelLoading();
    if (success) {
      save(isSciSleep, value);
    }
    else {
      state.isSciSleep.value = !value;
    }
  }
}
